package org.tuling.tlmallordersentineldemo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.tuling.tlmallcommon.BusinessException;
import org.tuling.tlmallcommon.Result;
import org.tuling.tlmallordersentineldemo.service.OrderService;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author: Fox
 * @Desc:
 **/

@RestController
public class TestController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/test1/{id}")
    public Result<?> test1(@PathVariable("id") Integer id){

        Result<?> res = null;
        try {
            res = orderService.getOrderById(id);
        }
        catch (BusinessException e) {
            return Result.failed(e.getMessage());
        }
        return res;
    }

    @GetMapping("/test2/{id}")
    public Result<?> test2(@PathVariable("id") Integer id){

        Result<?> res = null;
        try {
            res = orderService.getOrderById(id);
        }
        catch (BusinessException e) {
            return Result.failed(e.getMessage());
        }
        return res;
    }


    @RequestMapping("/test")
    public String test() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "========test()========";
    }

    AtomicInteger atomicInteger = new AtomicInteger(0);
    @RequestMapping("/test2")
    public String test222() {
        atomicInteger.getAndIncrement();
        if (atomicInteger.get() % 2 == 0){
            //模拟异常和异常比率
            int i = 1/0;
        }

        return "========test2()========";
    }


}
